﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Kingdee.YZJ.Linkerp.Demo.Model;
using Kingdee.YZJ.Linkerp.Demo.Service;
using Newtonsoft.Json;

using Oracle.DataAccess;

using Oracle.DataAccess.Client;
namespace dlreport001
{
    public partial class mainfrm : Form
    {
        //https://gitee.com/szlaolu/linkerp-doc/blob/master/openapi.md

        private AccessTokenService accessTokenService = new AccessTokenService();
        private OpenApiService openApiService = new OpenApiService();

        private string AppId =  Properties.Settings.Default.AppId ;
        private string Secret = Properties.Settings.Default.Secret;
        private string AesKey = Properties.Settings.Default.AesKey;
        private string eid =    Properties.Settings.Default.EID;

        private string uuid="";

       // private string connString = "User Id=sxdleasdb;password=sxdleasdb;Data Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.20)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = esdldb)))";


        public mainfrm()
        {
            InitializeComponent();
            comboBox1.DisplayMember = "name";
            comboBox1.ValueMember = "id";
        }


        private void button2_Click(object sender, EventArgs e)
        {
            //数据库连接
            //OrclQueryExecInterFace orcl = new OrclQueryExecInterFace(connString);

            //orcl.OrclQueryRestDataSet(textBox1.Text);

            //dataGridView1.DataSource = orcl.OrclQueryRestDataSet(textBox1.Text);

            //TestCreateReportTableInfoHeader();

            uuid =  Guid.NewGuid().ToString();

            TestSaveReportTableInfoData();
            TestGetReportTableInfoDataSaveStatus();


            MessageBox.Show("ok");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //GetAccessToken();
            TestGetAdminTableInfos();

            MessageBox.Show("ok");  

        }


        /// 获取轻应用管理员权限内所有报表分类及OpenApi创建的数据表信息
        public void TestGetAdminTableInfos()
        {
            AccessToken accessToken = accessTokenService.GetAccessToken();
            OpenApiService openApiService = new OpenApiService();
            //string eid = "525738"; //需要修改为实际工作圈eid

            List<CategoryWithReportTableVo> categoryWithReportTableVos = openApiService.GetAdminTableInfos(eid, accessToken.accessToken);

            string jsonData = JsonConvert.SerializeObject(categoryWithReportTableVos);

            comboBox1.DataSource = categoryWithReportTableVos;

            Console.WriteLine(jsonData);

            tbox.AppendText(jsonData);

        }

        //获取令牌
        public Boolean GetAccessToken()
        {
            AccessToken accessToken = accessTokenService.GetAccessToken();

            if (accessToken == null)
            {
                
                tbox.AppendText("GetAccessToken error!" + "<br>");
                return false;
            }
                
     
           tbox.AppendText(accessToken.ToString());

            return true;

            //Console.WriteLine(accessToken);
        }


        //刷新令牌
        public void RefreshToken()
        {
            AccessToken accessToken = accessTokenService.RefreshToken();
            tbox.AppendText(accessToken.ToString());
            Console.WriteLine(accessToken);

        }


        /// 创建数据表头
        /// </summary>
        public void TestCreateReportTableInfoHeader()
        {
            AccessToken accessToken = accessTokenService.GetAccessToken();

            TableInfoHeaderVo header = new TableInfoHeaderVo();
            header.eid = eid; //"13373723"; //需要修改为实际工作圈eid
            header.name = "销售日报";
            header.reportCategoryId = "5b0f49e791920e060d3d8291"; // 报表分类Id,需要修改为AdminTableInfos获取的报表分类ID

            List<HeaderItemVo> headerItems = new List<HeaderItemVo>();// 数据表头第一个字段类型必须为string且名称为recordId
            headerItems.Add(new HeaderItemVo("recordId", "string"));
            headerItems.Add(new HeaderItemVo("表头1", "string"));
            headerItems.Add(new HeaderItemVo("表头2", "number"));
            headerItems.Add(new HeaderItemVo("表头3", "timestamp"));
            header.headerItems = headerItems;

            TableInfoIdParam tableInfoIdParam = openApiService.CreateReportTableInfoHeader(header, accessToken.accessToken);
            Console.WriteLine(tableInfoIdParam);
        }

        /// <summary>
        /// 上传数据表数据
        /// </summary>
        public void TestSaveReportTableInfoData()
        {
            AccessToken accessToken = accessTokenService.GetAccessToken();
            TableInfoDataVo tableData = new TableInfoDataVo();

            // recordId为数据编号为必录项且必须唯一，如果上传数据与原有数据重复，执行更新操作
            string testData = "[{\"recordId\":\"3000000001\",\"表头1\":\"中国\",\"表头2\":\"100\",\"表头3\":\"2008-08-08 20:00:00\"},{\"recordId\":\"200000001\",\"表头1\":\"美国\",\"表头2\":\"80\",\"表头3\":\"2009-09-08 20:00:00\"},{\"recordId\":\"200000002\",\"表头1\":\"法国\",\"表头2\":\"115\",\"表头3\":\"2008-08-08 20:00:00\"}]";
            tableData.eid = eid; //需要修改为实际工作圈eid
            tableData.reportCategoryId = "5b0f49e791920e060d3d8291"; // 来源于AdminTableInfos,与表头保持一致
            tableData.tableInfoId = "5b0faa2c870476033394dc98";// 来源于表头创建或已有数据表Id
            tableData.data = testData;
            tableData.actionId = uuid; //"suqabcZciRLpwzxcvjbD111";// 操作Id,每个操作Id对应多个批次ID，建议使用uuid
            tableData.batchId = uuid; //"GmdGyrsuqabcZciRLpwzxcvjbDXu900";// 批次id,每次传不重复随机值，建议使用uuid

            bool status = openApiService.SaveReportTableInfoData(tableData, accessToken.accessToken);

            Console.WriteLine(status);
        }

        /// <summary>
        /// 查询上传数据表数据上传结果
        /// </summary>
        public void TestGetReportTableInfoDataSaveStatus()
        {
            AccessToken accessToken = accessTokenService.GetAccessToken();

            //string eid = "13373723"; //需要修改为实际工作圈eid

            string batchIds = uuid; //"GmdGyrsuqabcZciRLpwzxcvjbDXu900";// 批次id,与SaveReportTableInfoData批次id相同。支持批量上传以","分隔

            List<DataSaveStatus> statusList = openApiService.GetReportTableInfoDataSaveStatus(eid, batchIds, accessToken.accessToken);

            foreach (DataSaveStatus status in statusList)
            {
                Console.WriteLine(status);
            }
        }

        /// <summary>
        /// 生成图表数据
        /// </summary>
        public void TestGenerateDiagramData()
        {
            AccessToken accessToken = accessTokenService.GetAccessToken();

            string eid = "13373723"; //工作圈eid

            string tableInfoId = "5af42ebb5ce367326fa7a180";// 已有数据表Id
            string actionId = "suqabcZciRLpwzxcvjbD"; //操作ID
            bool status = openApiService.GenerateDiagramData(eid, tableInfoId, actionId, accessToken.accessToken);

            Console.WriteLine(status);
        }

        /// <summary>
        /// 清空数据表数据
        /// </summary>
        public void TestClearTableInfoData()
        {
            AccessToken accessToken = accessTokenService.GetAccessToken();

            string eid = "13373723"; //工作圈eid

            string tableInfoId = "5af42ebb5ce367326fa7a180";// 已有数据表Id

            bool status = openApiService.ClearTableInfoData(eid, tableInfoId, accessToken.accessToken);

            Console.WriteLine(status);
        }

        private void bindingSource1_CurrentChanged(object sender, EventArgs e)
        {

        }


    }
}
